﻿using System;
using System.Collections.Generic;
using System.Text;

using SugarWebApi.Commands;

namespace SugarWebApi.Processors
{

	public class SoapV1Processor : SoapProcessor
	{

		#region Constructors

		public SoapV1Processor(
			string url) : base(url)
		{
			this._soapV1 = new SugarSoapV1.sugarsoap();
		}

		#endregion

		#region Fields

		private readonly SugarSoapV1.sugarsoap _soapV1;

		#endregion

		#region Propreties

		public virtual string SoapEndPoint
		{
			get 
			{
				string format = (this.Url.EndsWith("/")) ? "" : this.Url + "/";
				format += "{0}soap.php";
				return string.Format(format, this.Url);
			}
		}

		#endregion

		protected override void OnUrlChanged(EventArgs e)
		{
			this._soapV1.Url = this.SoapEndPoint;
		}

		protected override LoginResult ProcesLoginCommand(
			LoginCommand command)
		{
			SugarSoapV1.user_auth auth = new SugarWebApi.SugarSoapV1.user_auth();
			auth.user_name = command.Parameters.Username;
			auth.password = command.Parameters.PasswordDigest;

			SugarSoapV1.set_entry_result soapresult =
				this._soapV1.login(
					auth,
					"SugarWebApi");

			LoginResult result = new LoginResult(
				command,
				"0".Equals(soapresult.error.number),
				soapresult.id);

			return result;
		}

		protected override GetEntryListResult ProcesGetEntryListCommand(GetEntryListCommand command)
		{
			SugarSoapV1.get_entry_list_result soapresult =
				this._soapV1.get_entry_list(
				command.SessionId,
				command.Module,
				command.Query,
				command.OrderBy,
				command.Offset,
				command.Fields,
				command.Limit,
				(command.Deleted) ? 1 : 0);
		
			SugarIndexResolver resolver = new SugarIndexResolver(command.Fields);
			GetEntryListResult result = new GetEntryListResult(
				command,
				"0".Equals(soapresult.error.number),
				command.Fields,
				SoapV1Translator.ToSugarEntries(soapresult.entry_list, resolver));

			return result;
		}

		protected override GetAvailableModulesResult ProcesGetAvailableModeulsCommand(
			GetAvailableModulesCommand command)
		{
			SugarSoapV1.module_list soapresult =
				this._soapV1.get_available_modules(
					command.Parameters.SessionId);

			GetAvailableModulesResult result = new GetAvailableModulesResult(
				command,
				!(soapresult.modules != null),
				SoapV1Translator.ToSugarModules(command.Conext, soapresult.modules));

			return result;
		}

		protected override LogoutResult ProcesLogoutCommand(LogoutCommand command)
		{
			this._soapV1.logout(
					command.SessionId);

			LogoutResult result = new LogoutResult(
				command,
				true);

			return result;
		}

		protected override GetModuleFieldsResult ProcesGetModuleFieldsCommand(
			GetModuleFieldsCommand command)
		{
			SugarSoapV1.module_fields soapresult =
				this._soapV1.get_module_fields(
					command.SessionId,
					command.Module);

			GetModuleFieldsResult result = new GetModuleFieldsResult(
				command,
				!(string.IsNullOrEmpty(soapresult.module_name)),
				SoapV1Translator.ToSugarFields(command.Conext, soapresult.module_fields1));

			return result;
		}

		protected override SetEntryResult ProcesSetEntryCommand(
			SetEntryCommand command)
		{
			SugarSoapV1.set_entry_result soapresult =
				this._soapV1.set_entry(
					command.SessionId,
					command.Module,
					SoapV1Translator.ToNameValues(command.Values));

			SetEntryResult result = new SetEntryResult(
				command,
				"0".Equals(soapresult.error.number),
				soapresult.id);

			return result;
		}
	}
}
